/**
* Copyright (c) 2012-2013 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners. 
* See LICENSE.TXT for license information.
*/

package com.nokia.example.paint.helpers;

import com.nokia.example.paint.Main;
import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Image;
import java.util.Timer;
import java.util.TimerTask;

/*
 * Helper class to handle file saving
 * in a separate thread.
 */
public class FileHandler {

    public FileHandler() {
    }

    /**
     * Save the image in draw area.
     * @param filename user given name to image
     * @param image created image
     * @param quitAfterSave should the application exit after save (if gone to save through exit dialog)
     */
    public static void saveImageToFile(final String filename, final Image image, final boolean quitAfterSave) {
        new Thread() {

            public void run() {

                if (filename.length() > 0) {

                    FileConnection con = null;
                    OutputStream out = null;
                    try {
                        con = (FileConnection) Connector.open(
                                System.getProperty("fileconn.dir.photos") + filename + ".bmp");
                        if (con != null) {
                            Main.getInstance().showProgressAlert();
                            Main.getInstance().updateProgressText("Saving...");
                            if (con.exists()) {
                                con.delete();
                            }
                            con.create();
                            out = con.openOutputStream();
                            BMPEncoder enc = new BMPEncoder(image);
                            enc.writeImageToFile(out);
                            Main.getInstance().updateProgressText("Image saved");
                            if (quitAfterSave) {
                                Main.getInstance().exit();
                            }
                        }
                    }
                    catch (Exception e) {
                        Main.getInstance().showError("Unable to save.");
                    }
                    finally {
                        try {
                            if (out != null) {
                                out.close();
                            }
                            if (con != null) {
                                con.close();
                            }
                        }
                        catch (IOException io) {
                            // Failed to close connections, but nothing we can do about that.
                        }
                    }

                    Timer timer = new Timer();
                    TimerTask closeDialog = new TimerTask() {

                        public void run() {
                            Main.getInstance().hideProgressAlert();
                        }
                    };
                    timer.schedule(closeDialog, 1600);
                }
                else {
                    Main.getInstance().showError("Please give filename.");
                }
            }
        }.start();
    }
}
